import json
import math

from django.db.models import Count
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

from train.a import fn
from . models import *

# Create your views here.


def add_views(request):
    # Author.objects.create(name='王老师',age=32,email='wang.wc@163.com')
    # obj1 = Author(name='隔壁老王',age=32,email='laowang.gebi@163.com')
    # obj1.save()
    # dic = {
    #   'name':'超哥哥',
    #   'age': 32,
    #   'email': 'brother_chao@163.com',
    # }
    # obj = Author(**dic)
    # obj.save()

    # Publisher.objects.create(name='北京大学出版社',address='北大东路125号',city='北京',country='中国',website='http://www.beida.com')
    #
    # obj = Publisher(name='清华大学出版社',address='清华东路125号',city='北京',country='中国',website='http://www.qinghua.com')
    # obj.save()
    #
    # dic = {
    #   'name':'人民教育出版社',
    #   'address':'珠市口大街',
    #   'city':'北京',
    #   'country':'中国',
    #   'website':'http://www.people.com'
    # }
    # obj1 = Publisher(**dic)
    # obj1.save()

    Book.objects.create(title='老王的一天', publicate_date='2015-10-12')

    book = Book(title='老王的一生', publicate_date='2017-09-12')
    book.save()

    dic = {
        'title': '王老师的幸福生活',
        'publicate_date': '2018-01-01'
    }
    book1 = Book(**dic)
    book1.save()
    return HttpResponse('ok')


def oto_views(request):
    # wife = Wife()
    # wife.name = '王夫人'
    # wife.age = 26
    # # 直接指定author_id的值(author_id是由OneToOneField隐式增加的属性)
    # wife.author_id =
    # wife.save()

    # wife = Wife()
    # wife.name = "超夫人"
    # wife.age = 18
    # author = Author.objects.get(name='超哥哥')
    # wife.author = author
    # wife.save()

    # 正向查询
    wife = Wife.objects.get(id=1)
    wife_author = wife.author
    print(wife_author.name)
    fn(wife_author)
    # 反向查询
    author = Author.objects.get(id=1)
    author_wife = author.wife
    print(author_wife.name)
    return HttpResponse('OK')


def otm_views(request):
    # 正向查询：通过Book查询Publisher,在15-otm.html中显示每个book对应的publisher
    b = Book.objects.filter(title='sad死你').first()
    if b:
        print(b)
    # b.title = '想死你'
    # b.save()

    # books = Book.objects.all()
    # for x in books:
    #     publisher = x.publisher
    #     print(publisher.name)
    # 反向查询
    # pub = Publisher.objects.get(id=1)
    # print(request.session['id'])
    # request.session['id'] = 'yangxin'

    # pub_books = pub.book_set.all().aggregate(books=Count('title'))
    # print(pub_books['books'])
    # for d in pub_books:
    #     print(d.title)
    # nums = Publisher.objects.aggregate(book_counts=Count('book'))
    # print(nums)
    # print(nums['book_counts'])

    return HttpResponse('OK')


def img_views(request):
    if request.method == 'GET':
        return render(request, 'img.html')
    else:
        img = Img()
        img.img = request.FILES.get('img')
        img.name = request.FILES.get('img').name
        img.save()
        image = Img.objects.get(name=request.FILES.get('img').name)
        print(image.name)
        dic = {
            'name': image.img
        }
        # return HttpResponse('OK')
        return render(request, 'show.html', dic)


def ce_views(request):
    if request.method == 'GET':
        return render(request, 'one.html')
    else:
        # uname = request.POST.get('uname', None)
        # uage = request.POST.get('uage', None)
        # print(uname, uage)
        uname = request.POST.get('ids')
        # print(uname)
        lst = json.loads(uname)
        print(lst)
        for i in lst:
            print(i)
        dic = {
            '1': 0
        }
        print(request.session['id'])
        return JsonResponse({"mingzi": '你是傻逼'})
        # return HttpResponse(json.dumps(dic))


def train_views(request):
    print(request.session['id'])
    # return HttpResponse('OK')
    return render(request, 'two.html')


# 确定查询经纬度范围
def __get_area(latitude, longitude, dis):
    """
    确定查询经纬度范围
    :param latitude:中心纬度
    :param longitude:中心经度
    :param dis:半径
    :return:(minlat, maxlat, minlng, maxlng)
    """
    r = 6371.137
    dlng = 2 * math.asin(math.sin(dis / (2 * r)) / math.cos(latitude * math.pi / 180))
    dlng = dlng * 180 / math.pi

    dlat = dis / r
    dlat = dlat * 180 / math.pi

    minlat = latitude - dlat

    maxlat = latitude + dlat

    minlng = longitude - dlng

    maxlng = longitude + dlng

    return minlat, maxlat, minlng, maxlng


def request_views(request):
  # print(dir(request))
  # print(request.META)
  scheme = request.scheme
  body = request.body
  path = request.path
  host = request.get_host()
  method = request.method
  get = request.GET
  post = request.POST
  cookies = request.COOKIES
  return render(request, '01-request.html', locals())